/*
 *  net_MPI.h
 *  net_MPI
 *
 *  Created by Basile Clout on 28/06/07.
 *  Copyright 2007 Basile Clout. All rights reserved.
 *
 */

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>	
#include <time.h>	
#include <getopt.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <math.h>								/* For computing deviation */

#define LENGTH 64                              /* length of strings */
#define NB_METHODS 4                               /* Number of different methods to determine bandwidth */

#define MAX_COEF_SIZE_T1 21                     /* Maximum value of the test sizes (Number of bit switchs) */
#define MAX_COEF_SIZE_T2 22
#define MAX_COEF_SIZE_T3 22 
#define MAX_COEF_SIZE_T4 22
#define MIN_COEF_SIZE 0

#define SKIP 2                                  /* Number of skiped tests */
#define NB_TESTS 5                             /* Number of TOTAL tests (REAL tests = TOTAL-SKIP) */
#define NB_REQUESTS 4                          /* Number of "threads" for isr functions */
#define NB_REQUESTS_MAX 32

#define stats 4
#define M_SR 0
#define M_BSR 1
#define M_ISR 2
#define M_BISR 3

#define MPI_REPLY 2
#define MPI_ORDER 1

/* Define a connection between the master and the corresponding node */
struct node {
	int master;
	int rank;
	struct latency *lat;
	struct bandwidth *bdw;
	struct node *next;
};

/* Describes the latency properties */
struct latency {
	const struct node *parent;
	double *array;
	int len;
	double max;
	double min;
	double avg;
	double stdev;
};

/* Describes the bandwidth properties */
struct bandwidth {
	const struct node *parent;
	double *array;
	int len;
	double max;
	double min;
	double avg;
	double stdev;
};

/* Describes the current configuration */
struct configuration {
	int myrank;
	int nb_tasks;
	int fl_debug;
	int fl_live;
	int method;
	int fl_help;
	int size;
	int lat_size;
	int master;
	int skip;
	int nb_tests;
	int isr_requests;
	struct node *head;

};



void *error_handle_MPI(int error, int myrank, char *description);
int net_MPI_bdw_sr(const struct configuration *conf, const struct node *_node, double *array_bdw);
int net_MPI_bdw_isr(const struct configuration *conf, const struct node *_node,
		double *array_bdw);

int displ_txt(FILE *stream, double **results, int max_coef, int nbtasks, int master_rank);
int displ_txtp(FILE *stream, double **results, int min_coef, int coefs,  int nbtasks, int master_rank);

int net_MPI_lat_bsr(const struct configuration *conf, const struct node *_node, double * array_lat);

int get_latency_master(const struct configuration *conf, const struct node *_node,
		struct latency *lat);
int get_bandwidth_master(const struct configuration *conf, const struct node *_node,
		struct bandwidth *bdw);

